package engine.filesystem;

import java.util.ArrayList;

import common.Log;
import engine.Constants;

public final class ResourceManager {
	public ResourceManager() {
		_Resources = new ArrayList<Resource_A>();
		_Scanner = new FileScanner(Constants.DATA_ZIPFILE, Constants.DATA_PATH);
		
	}
	
	private FileScanner _Scanner;
	
	public FileScanner getFileScanner() {
		return _Scanner;
	}
	public Resource_A aquireResource(String name) {
		Resource_A resource = lookup(name);
		if(resource == null) {
			Log.warning("Unable to aquire resource. " + name + " not found.");
			return null;
		}
		resource.aquire();
		return resource;
	}
	public void releaseResource(String name) {
		Resource_A resource = lookup(name);
		if(resource == null) {
			Log.warning("Unable to release resource. " + name + " not found.");
			return;
		}
		resource.release();
	}
	public void registerResource(Resource_A resource) {
		if(resource == null) {
			Log.warning("Rejected resource registration. Null resource");
			return;
		}
		
		if(lookup(resource.Name) != null) {
			Log.warning("Rejected resource registration. Duplicate name " + resource.toString());
			return;
		}
		
		if(resource.isValid() == false) {
			Log.warning("Rejected resource registration. Failed verification of " + resource.toString()); 
			return;
		}
		
		if(_Resources.add(resource) == false) {
			Log.debug("List full? Error registering new resource, " + resource.toString()); 
					
		} else {
			Log.debug("Registered new resource, " + resource.toString()); 
		}
	}
	
	private Resource_A lookup(String name) {
		for(Resource_A r : _Resources) {
			if(r.Name.equals(name)) {
				return r;
			}
		}
		return null;
	}
	
	private ArrayList<Resource_A> _Resources;
}